package com.jay.base.lock;

/**
 * Created by caoyuan7 on 2019/4/26.
 */
public class Cannot {
    public static void main(String args[]) {
        Count t = new Count();
        try {
            t.print();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
class Count{
    LockNo lock = new LockNo();
    public void print() throws InterruptedException {
        lock.lock();
        doAdd();
        lock.unlock();
    }
    public void doAdd() throws InterruptedException {
        lock.lock();
        //do something
        lock.unlock();
    }
}

class LockNo {
    private boolean isLocked = false;

    public synchronized void lock() throws InterruptedException {
        while (isLocked) {
            wait();
        }
        isLocked = true;
    }

    public synchronized void unlock() {
        isLocked = false;
        notify();
    }
}
